#!/bin/env python
#-*- coding:utf-8 -*-

#----------------------------------------------------------------------------
# Name     :      GUI.py
# Purpose  :      Mysql tool for ucweb, and this is the py file of gui.
# Author   :      Albert cheng
# Created  :      2012-10-29
# Copyright:     (c) 2012 by Albertcheng
# Licence  :      wxWindows license
#----------------------------------------------------------------------------

import wx
import string
from request_handler import request_sqlite

from base_panel import basePanel as bp

class ssdz_panel(bp):


    def __init__(self, parent = None, log = None):
        bp.__init__(self, parent, log)
        self.nb = parent
        self.log = log
        

        #--------------------------
        self.player_detail_ui_label = [
                                    ["UC账号".decode('utf-8'),"PlayerID","角色名".decode('utf-8'),"等级".decode('utf-8'),"经验值".decode('utf-8')],
                                    ["最近登陆时间".decode('utf-8'), "活力值".decode('utf-8'),"敏捷属性".decode('utf-8'),"力量属性".decode('utf-8')],
                                    ]
                
        self.player_detail_ui_label[1].append("速度属性".decode('utf-8'))
            
        self.player_detail_ui_label.append(["Vip到期时间".decode('utf-8'),"Vip等级".decode('utf-8'),"Vip经验".decode('utf-8')])
                                    
        self.player_detail_ui_label.append(["敏捷丸（小）".decode('utf-8'),"力量丸（小）".decode('utf-8'),"速度丸（小）".decode('utf-8'),"经验丸（小）".decode('utf-8')])
        self.player_detail_ui_label.append(["超级敏捷丸".decode('utf-8'),"超级力量丸".decode('utf-8'),"超级速度丸".decode('utf-8'),"超级经验丸".decode('utf-8')])
        self.player_detail_ui_label.append(["button|编辑".decode('utf-8'),"button|保存".decode('utf-8')])


                                    
        self.player_detail_ui_value_tag = [
                                    "t_player.uc_id","t_player.id","t_player.name","t_player.level","t_player.exp",
                                    "t_player.last_login_time","t_player.energy,t_player.max_energy","t_player.agility","t_player.power",
                                    "t_player.speed",
                                    "t_player.vip_deadline","t_player_misc.vip_exp","t_player_misc.vip_level",#"t_player.max_hp","t_player_prop.count.prop_id.8",
                                    "t_player_state.agility","t_player_state.power","t_player_state.speed","t_player_state.attack",
                                    "t_player_state.super_agility","t_player_state.super_power","t_player_state.super_speed","t_player_state.super_attack",
                                    "self.Evt_OnEditRoleSizer","self.Evt_OnSaveRoleSizer",
                                    ]
        #self.sizer_manager()
        
        self.base_sizer_manager()
        self.evt_manager()
        self.run_bg_mysql_thread("ssdz")
        
    def evt_manager(self):
        self.ssdz_search_v.Bind(wx.EVT_SEARCHCTRL_CANCEL_BTN,lambda e: self.ssdz_search_v.SetValue(''))
        self.ssdz_search_v.Bind(wx.EVT_TEXT_ENTER, self.OnSearch)
        

    def base_sizer_manager(self):
        
        self.main_sizer = wx.BoxSizer(wx.VERTICAL)
        
        search_sizer = wx.BoxSizer(wx.HORIZONTAL)
        self.search_t = wx.StaticText(self,-1,"查询您的UCID或者角色昵称：".decode('utf-8'))
        self.ssdz_search_v = wx.SearchCtrl(self,-1, size=(500,25),style=wx.TE_PROCESS_ENTER)
        self.ssdz_search_v.ShowCancelButton(True)
        
        search_sizer.Add(self.search_t,0,wx.LEFT|wx.BOTTOM|wx.ALL,5)
        search_sizer.Add(self.ssdz_search_v,0,wx.LEFT|wx.ALL,5)
        
        self.main_sizer.Add(search_sizer,0,wx.LEFT|wx.ALL,5)
        self.main_sizer.Add(wx.StaticLine(self,-1,size=(1500,1)),0,wx.LEFT|wx.ALL,5)
        
        self.sizer_manager()
        self.SetSizer(self.main_sizer)
        self.main_sizer.Fit(self.nb)
          
        
    def sizer_manager(self):
        tj_sizer = wx.BoxSizer(wx.VERTICAL)
        
        role_sizer = wx.BoxSizer(wx.VERTICAL)
        
        self.player_detail_sizer = wx.StaticBoxSizer(wx.StaticBox(self, -1, label="角色基础数据".decode('utf-8')), wx.VERTICAL)
        self.player_detail_ui = self.make_ui_code(self.player_detail_ui_label,self.player_detail_sizer)
        
        
        button_tags = [t for t in self.player_detail_ui.keys() if  "line_box_info_button_" in t]
        button_tags.sort()
        tags = [x.split('.')[1] for x in self.player_detail_ui_value_tag if x.strip() != "" and "self." in x.strip()]
        
        #for k in range(len(button_tags)):
            #self.Bind(wx.EVT_BUTTON, getattr(self,"%s" % tags[k]), self.player_detail_ui[button_tags[k]])

        #=======================================================================
        # grid
        #=======================================================================
        self.grid_sizer = wx.BoxSizer(wx.HORIZONTAL)
        #-------------------------------------------------
        propgrid_sizer = wx.StaticBoxSizer(wx.StaticBox(self, -1, label="角色道具数据".decode('utf-8')), wx.VERTICAL)
        self.item_g = self.make_grid_code(['MysqlID'.decode('utf-8'), '道具名'.decode('utf-8'),'道具ID'.decode('utf-8'), '道具数量'.decode('utf-8')])
        self.item_g.SetMinSize((320,300))
        #-------------------------------------------------
        self.weapongrid_sizer = wx.StaticBoxSizer(wx.StaticBox(self, -1, label="角色武器数据".decode('utf-8')), wx.VERTICAL)
        self.weapon_g = self.make_grid_code(['MysqlID'.decode('utf-8'), '武器名'.decode('utf-8'),'武器ID'.decode('utf-8'), '武器等级'.decode('utf-8')])
        self.weapon_g.SetMinSize((320,300))
        #-------------------------------------------------
        skillgrid_sizer = wx.StaticBoxSizer(wx.StaticBox(self, -1, label="角色技能数据".decode('utf-8')), wx.VERTICAL)
        self.skill_g = self.make_grid_code(['MysqlID'.decode('utf-8'), '技能名'.decode('utf-8'),'技能ID'.decode('utf-8'), '技能等级'.decode('utf-8')])
        self.skill_g.SetMinSize((320,300))
        

        
        #-------------------------------------------------
        item_ot = wx.Button(self,-1,"设置道具数量".decode('utf-8'))
        weapon_ot = wx.Button(self,-1,"设置武器等级".decode('utf-8'))
        skill_ot = wx.Button(self,-1,"设置技能等级".decode('utf-8'))
        
        #-------------------------------------------------
        #self.Bind(wx.EVT_BUTTON,self.OnOt_Prop,item_ot)
        #self.Bind(wx.EVT_BUTTON,self.OnOt_Weapon,weapon_ot)            
        #self.Bind(wx.EVT_BUTTON,self.OnOt_Skill,skill_ot)
        
        #-------------------------------------------------
        all_weapon = wx.Button(self,-1,"一键全武器设置等级".decode('utf-8'))
        self.all_weapon_lvl = wx.TextCtrl(self,-1,"1",size=((50,20)))                
        all_skill = wx.Button(self,-1,"一键全技能设置等级".decode('utf-8'))
        self.all_skill_lvl = wx.TextCtrl(self,-1,"1",size=((50,20)))
                            
        weaponall_sizer = wx.BoxSizer(wx.HORIZONTAL)
        skillall_sizer = wx.BoxSizer(wx.HORIZONTAL)
        
        skillall_sizer.Add(all_skill,0,wx.LEFT|wx.ALL,5)
        skillall_sizer.Add(self.all_skill_lvl,0,wx.LEFT|wx.ALL,5) 

        weaponall_sizer.Add(all_weapon,0,wx.LEFT|wx.ALL,5)
        weaponall_sizer.Add(self.all_weapon_lvl,0,wx.LEFT|wx.ALL,5)
        
          
        propgrid_sizer.Add(self.item_g,0,wx.EXPAND,5)
        propgrid_sizer.Add(item_ot,0,wx.EXPAND,5)
        
        self.weapongrid_sizer.Add(self.weapon_g,0,wx.EXPAND,5)
        self.weapongrid_sizer.Add(weapon_ot,0,wx.EXPAND,5)
        self.weapongrid_sizer.Add(weaponall_sizer,0,wx.EXPAND,5)
        
        skillgrid_sizer.Add(self.skill_g,0,wx.EXPAND,5)
        skillgrid_sizer.Add(skill_ot,0,wx.EXPAND,5)
        skillgrid_sizer.Add(skillall_sizer,0,wx.EXPAND,5)
        
      
        
                
        self.grid_sizer.Add(propgrid_sizer,0,wx.EXPAND,5)
        self.grid_sizer.Add(skillgrid_sizer,0,wx.EXPAND,5)
        
        self.grid_sizer.Add(self.weapongrid_sizer,0,wx.EXPAND,5)
   
        #self.Bind(wx.EVT_BUTTON,self.Evt_AllSkillOT,all_skill)
        
        
        tj_sizer.Add(self.player_detail_sizer,0, wx.EXPAND, 5)
        tj_sizer.Add(role_sizer,0, wx.EXPAND, 5)
        tj_sizer.Add(self.grid_sizer,0, wx.EXPAND, 5)
        
        self.main_sizer.Add(tj_sizer)
        
        
    def ssdz_refresh_gui(self,dr):
        try:
            self.proj = self.proj_def[int(self.nb.GetSelection())]
        except:
            wx.MessageBox("项目数据初始化错误，无法查询".decode("utf-8"),"Error",wx.OK)
            return
        if self.proj == "config":
            setattr(self.nb,"ssdz_refresh_mysql_flag",False)
            setattr(self.nb,"tianjie_refresh_mysql_flag",False)
            return
        if getattr(self.nb,"ssdz_refresh_mysql_flag"):
            self.Log.log("refreshing gui for proj [%s]" % self.proj )
            if self.player == {}:
                return
            self.refresh_xml()
            
            self.refresh_data_player_detail_ui()
            self.refresh_data_player_skill_ui()
            self.refresh_data_player_prop_ui()
            #self.refresh_data_player_equip_ui()
            




                
    
    def refresh_data_player_equip_ui(self):
        equip_colors = {
                        1:"白色",
                        2:"蓝色",
                        3:"紫色",
                        4:"橙色",
        }
        
        show_data = []
        try:
            equips_data = self.player[self.tb_player_equip]
        except:
            equips_data = {}
        
        if equips_data != {}:
            k = equips_data.keys()
            k.sort()
            for n in k:
                m = equips_data[n]
                k1 = m.keys()
                equip_xml = self.equips_cfg[m[[x for x in k1 if "equip" in x.lower() and "id" in x.lower()][0]]]
                equip_color =  equip_xml.attrib["color"]
                
                equipname = equip_xml.attrib["name"] +" - " + equip_colors[int(equip_color)] 
                
                show_data.append((m[[x for x in k1 if "id" == x.lower()][0]],\
                                  equipname,\
                                  m[[x for x in k1 if "equip" in x.lower() and "id" in x.lower()][0]],\
                                  m[[x for x in k1 if "extras" == x.lower()][0]]))
        
        show_data.sort(lambda x,y:cmp(int(x[2]),int(y[2])))
        for n in range(len(show_data),99):
            show_data.append(("","","",""))
        for nrow in range(len(show_data)):
            for ncol in range(len(show_data[nrow])):
                self.equip_g.SetValue(nrow,ncol,str(show_data[nrow][ncol]).decode('utf-8'))